package com.ding.authorizationserver.service

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service

@Service
class AuthUserDetailsService implements UserDetailsService{

    @Autowired
    PasswordEncoder passwordEncoder

    @Override
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (username!='shi')
            return null

        return new UserDetails() {

            @Override
            Collection<? extends GrantedAuthority> getAuthorities() {

                List<GrantedAuthority> list = new ArrayList<>()
                list.add new SimpleGrantedAuthority('USER')
                return list
            }

            @Override
            String getPassword() {  //返回的是使用编码器编码后的密码

                return passwordEncoder.encode('123')
            }

            @Override
            String getUsername() {
                return username
            }

            @Override
            boolean isAccountNonExpired() {
                return true
            }

            @Override
            boolean isAccountNonLocked() {
                return true
            }

            @Override
            boolean isCredentialsNonExpired() {
                return true
            }

            @Override
            boolean isEnabled() {
                return true
            }
        }
    }
}
